package com.eian.boot.module.system.post.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.eian.boot.common.constants.PermissionConstants;
import com.eian.boot.common.core.model.CommonResult;
import com.eian.boot.module.system.associations.user.post.service.UserPostRelationService;
import com.eian.boot.module.system.post.model.dto.*;
import com.eian.boot.module.system.post.model.vo.PostDetailVO;
import com.eian.boot.module.system.post.model.vo.PostSimpleVO;
import com.eian.boot.module.system.post.model.vo.PostVO;
import com.eian.boot.module.system.post.service.SysPostService;
import com.eian.boot.mybatis.core.page.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 岗位管理控制器
 *
 * @author alex.meng
 */
@Tag(name = "岗位管理")
@RestController
@RequestMapping("/system/post")
@AllArgsConstructor
public class PostController {

    private final SysPostService postService;
    private final UserPostRelationService userPostRelationService;

    @Operation(summary = "创建岗位")
    @SaCheckPermission(PermissionConstants.Post.CREATE)
    @PostMapping("/create")
    public CommonResult<String> create(@Valid @RequestBody PostCreateRequest request) {
        postService.createPost(request);
        return CommonResult.ok("岗位创建成功");
    }

    @Operation(summary = "分页查询岗位列表")
    @SaCheckPermission(PermissionConstants.Post.LIST)
    @GetMapping("/list")
    public CommonResult<PageResult<PostVO>> list(@Valid PostQueryRequest request) {
        return CommonResult.ok(postService.getPostList(request));
    }

    @Operation(summary = "获取岗位详情")
    @SaCheckPermission(PermissionConstants.Post.QUERY)
    @GetMapping("/{id}")
    public CommonResult<PostDetailVO> getDetail(@Parameter(description = "岗位ID") @PathVariable Long id) {
        return CommonResult.ok(postService.getPostDetail(id));
    }

    @Operation(summary = "更新岗位信息")
    @SaCheckPermission(PermissionConstants.Post.UPDATE)
    @PostMapping("/update")
    public CommonResult<String> update(@Valid @RequestBody PostUpdateRequest request) {
        postService.updatePost(request);
        return CommonResult.ok("岗位更新成功");
    }

    @Operation(summary = "删除岗位")
    @SaCheckPermission(PermissionConstants.Post.DELETE)
    @PostMapping("/delete/{id}")
    public CommonResult<String> delete(@Parameter(description = "岗位ID") @PathVariable Long id) {
        postService.deletePost(id);
        return CommonResult.ok("岗位删除成功");
    }

    @Operation(summary = "启用/禁用岗位")
    @SaCheckPermission(PermissionConstants.Post.STATUS)
    @PostMapping("/update-status")
    public CommonResult<String> updateStatus(@Valid @RequestBody PostStatusUpdateRequest request) {
        postService.updatePostStatus(request.getId(), request.getStatus());
        return CommonResult.ok("岗位状态更新成功");
    }

    @Operation(summary = "分配用户岗位")
    @SaCheckPermission(PermissionConstants.Post.ASSIGN_USER)
    @PostMapping("/assign-user")
    public CommonResult<String> assignUser(@Valid @RequestBody UserPostAssignRequest request) {
        userPostRelationService.assignUserPosts(request.getUserId(), request.getPostIds());
        return CommonResult.ok("用户岗位分配成功");
    }

    @Operation(summary = "获取所有岗位简单列表（下拉选择）")
    @SaCheckPermission(PermissionConstants.Post.LIST)
    @GetMapping("/simple-list")
    public CommonResult<List<PostSimpleVO>> getSimpleList() {
        return CommonResult.ok(postService.getAllSimplePosts());
    }

    @Operation(summary = "获取岗位下的用户ID列表")
    @SaCheckPermission(PermissionConstants.Post.QUERY)
    @GetMapping("/{id}/users")
    public CommonResult<List<Long>> getPostUsers(@Parameter(description = "岗位ID") @PathVariable Long id) {
        return CommonResult.ok(userPostRelationService.getUserIdsByPostId(id));
    }
}

